<template>
  <el-row style="margin:15px">
    <el-form inline :model="searchForm">
      <el-form-item label="客户编码">
        <el-input v-model="searchForm.code" placeholder="客户编码" />
      </el-form-item>
      <el-form-item label="客户名称">
        <el-input v-model="searchForm.name" placeholder="客户名称" />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status" style="width:100px" placeholder="请选择状态" clearable>
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="loadCustomerList">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-row>
  <el-row style="margin:15px">
    <el-radio-group v-model="selectedCustomerCode" style="width: 100%">
      <el-table
          :data="customerList"
          stripe
          border
          width="100%"
      >
        <el-table-column>
          <template #default="scope">
            <el-radio :value="scope.row.code" />
          </template>
        </el-table-column>
        <el-table-column prop="code" label="客户编码" />
        <el-table-column prop="name" label="客户名称" />
        <el-table-column prop="contactPerson" label="联系人" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status == '1' ? 'success' : 'danger'">
              {{ scope.row.status == '1' ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-radio-group>
  </el-row>
  <el-row>
    <el-button @click="doSelectCustomer">确定</el-button>
  </el-row>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { getCustomerPage } from "@/api/customer.js";

const searchForm = ref({
  code: '',
  name: '',
  status: ''
});
const selectedCustomerCode = ref(-1);
const customerList = ref([]);

const emit = defineEmits(["selectCustomer"]);

const loadCustomerList = () => {
  // 使用分页参数1和10000获取全部数据，保持与原物料页面逻辑一致
  getCustomerPage(1, 10000, searchForm.value).then(res => {
    customerList.value = res.data.list;
  })
};

loadCustomerList();

const doSelectCustomer = () => {
  console.log('当前选中的客户编码:', selectedCustomerCode.value);

  if (!selectedCustomerCode.value) {
    ElMessage.warning('请先选择一个客户');
    return;
  }

  console.log('触发selectCustomer事件');
  emit("selectCustomer", selectedCustomerCode.value);
};

onMounted(() => {
  loadCustomerList();
});
</script>

<style scoped>

</style>